$(function(){
    var page=1;
    var limit=10;
    var nickname="";
    var username="";
    var mobile="";
    var total=0;

    getUserList(page);

    // 获取用户列表
    function getUserList(page){
        let requestData={"page":page,"limit":limit,"username":username,"nickname":nickname,"mobile":mobile};
        $.ajax({
            type: "get",
            url: basePath+'/admin/users',
            data: requestData,
            async: false,
            dataType: "json",
            //这里是重点
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("token", auth.token);
            },
            success: function (res) {
                debugger;
                total=res.total;
                // 计算总页数
                var totalPage=Math.ceil(total / limit);
                if(res.code=="00000"){
                    var userArr=[];
                    let uList=res.data;
                    for(var k=0;k<uList.length;k++){
                        userArr.push("<tr>");
                        userArr.push("<td><input type='checkbox' name='ids' lay-skin='primary'></td>");
                        userArr.push("<td class='hidden-xs'>"+uList[k].id+"</td>");
                        userArr.push("<td class='hidden-xs'>"+uList[k].username+"</td>");
                        userArr.push("<td>"+uList[k].nickname+"</td>");
                        userArr.push("<td><img class='avatar-icon' src='"+uList[k].avatar+"'/></td>");
                        userArr.push("<td class='hidden-xs'>"+uList[k].mobile+"</td>");
                        var gender=uList[k].gender==0?"男":"女";
                        userArr.push("<td class='hidden-xs'>"+gender+"</td>");
                        var status=uList[k].status==0?"开启":"禁用";
                        var statusBnt;
                        if(uList[k].status==0){
                            statusBnt="<button class='layui-btn layui-btn-xs layui-btn-normal table-list-status' data-status='"+uList[k].status+"'>"+status+"</button>";
                        }else{
                            statusBnt="<button class='layui-btn layui-btn-xs layui-btn-warm table-list-status' data-status='"+uList[k].status+"'>"+status+"</button>";
                        }
                        userArr.push("<td data-id='"+uList[k].id+"'>"+statusBnt+"</td>");
                        userArr.push("<td><div class='layui-inline'><button class='layui-btn layui-btn-normal layui-btn-sm go-btn' data-id='"+uList[k].id+"' data-url='user-info.html'><i class='layui-icon'>&#xe642;</i></button><button class='layui-btn layui-btn-danger layui-btn-sm del-btn' data-id='"+uList[k].id+"'><i class='layui-icon'>&#xe640;</i></button></div></td>");
                        userArr.push("</tr>");
                    }
                    userArr.join('');
                    $("#user-main").html(userArr);
                }
            },
            error:function(error){console.log(error);}
        });
    };
});
